var base_url="http://"+location.host+"/";
var total_page=0,total_num=0,page=0,catalog_id=0;
catalog_id=$(".catalog_list").attr("data_catalog_id");
book_id=$(".catalog_list").attr("data_id");
function get_catalog(page){
    var num=50;
    var order="asc";

    req_url=base_url+"api/catalog/"+book_id;
    $.ajax({
        url:req_url,
        type:"POST",
        data:{
            page:page,
            num:num,
            order:order
        },
        success:function(res){
            //console.log(res);
            total_page=res.total_page;
            total_num=res.total_num;
            if(res.page>res.total_page){//最后一页
                $(".nomore").show();
                return false;
            }
            var html="";
            res.data.forEach(function(v,i){
                if(v.vip=="Y"){
                   html+='<a href="/catalog_detail/'+book_id+"/"+v.id+"?page="+res.page+'" class="weui-media-box weui-media-box_appmsg catalog_item" id="cid'+v.id+'"><div class="weui-media-box__hd"></div><div class="weui-media-box__bd"><h1 class="weui-media-box__title"><h4>'+v.name+'<b style=\'color:red;float:right\'>vip</b>'+'</h4></h1></div></a>';
                }else{
                   html+='<a href="/catalog_detail/'+book_id+"/"+v.id+"?page="+res.page+'" class="weui-media-box weui-media-box_appmsg catalog_item" id="cid'+v.id+'"><div class="weui-media-box__hd"></div><div class="weui-media-box__bd"><h1 class="weui-media-box__title"><h4>'+v.name+'</h4></h1></div></a>';
                }

            });
            $(".catalog_list").html(html);
            $(".catalog_list").attr("data_page",res.page+1);
            return true;
        }
    });
}

$.ajaxSetup({
    async:false,
});
page=$(".catalog_list").attr("data_page");
get_catalog(page);
$.ajaxSetup({
    async:true,
});


//滚动加载数据
//$(document.body).infinite(100).on("infinite",function(){
    //page=$(".catalog_list").attr("data_page");
    //get_catalog(page);
//});


items=[];
for(let i=0;i<total_page;i++){
    let str="";
    if((i+1)*50>total_num){
        str=i*50+"-"+total_num;
    }else{
        str=i*50+"-"+(i+1)*50;
    }
    items.push({
        title:str,
        value:i
    });
}

//console.log(total_num,total_page,list);
$("#catalog").select({
                title:"章节目录",
                items:items,
                onChange:function(e){
                    //console.log(e);
                    var page=parseInt(e.values)+1;
                    if(page==undefined){
                        return;
                    }
                    get_catalog(page);
                }
});

//滚动到指定章节
if(catalog_id!=="0"){
    var item={};
    $(".catalog_item").each(function(i){
        if($(this).attr("id")=="cid"+catalog_id){
            item=$(this);
            $(this).css({
                "color": "#080808",
                "background": "#8F8F8F",
                "font-weight":"bolder",

            });
        }
    });
    c = item.offset().top;    //元素距离文档（document）顶部的高度
    $(window).scrollTop(c);   //页面滚动的高度
    //console.log(c);
}